#include "mainwindow.h"

/* QT库 */
#include <QApplication>
#include <QPushButton>
#include <QGuiApplication>
#include <QScreen>

//#define NOT_TRANSMIT

/* 标准库 */
#include <iostream>

/* 私人接口 */
#include <WebAPI/webTCP.h>
#include "test.h"

/* 全局变量 */
TCP * TCPer = nullptr;

unsigned char sendBuffer[4147200];

/* 槽函数 */
/**
 * @brief 单次抓取全屏
 */
void slotGrabFullScreen()
{

    for(auto i =0; i<319800;i++)
        gImage_AllZeros[i] = i /1500;

    QScreen *screen = QGuiApplication::primaryScreen(); // 屏幕对象

    QString filePathName = "./screenShotCache";
    filePathName += ".jpg";

    QPixmap pix = screen->grabWindow(0);    // 抓一次屏幕

    if(!(pix.save(filePathName, "jpg")))    // 保存到本地
    {
        std::cout<<"save full screen failed"<<std::endl;
    }

    // 转换为RGB16（5-6-5）位图
    QImage img= pix.toImage().convertToFormat(QImage::Format_RGB16);
    auto imgSize = img.size();  // 图片大小
    std::cout <<"width:" <<imgSize.width()
             <<"height:"<< imgSize.height()<<std::endl
            <<"pix format:"<< img.format()<<std::endl;

    const uchar *date = img.constBits();    // 图片数据
    int dataSize = imgSize.width()*imgSize.height()*2;
    for(int _i = 0;_i<dataSize;_i++)
        sendBuffer[_i] = date[_i];
#ifndef NOT_TRANSMIT
    TCPer->sendCmd((const unsigned char *)&dataSize,sizeof (dataSize));
    TCPer->sendData(sendBuffer,dataSize);
//    dataSize = sizeof(gImage_AllZeros);
//    TCPer->sendCmd((const unsigned char *)&dataSize,sizeof (dataSize));
//    TCPer->sendData(gImage_AllZeros,dataSize);

#endif
}

void quitProg(void)
{
    free(TCPer);
    return;
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv); //
//    MainWindow w;
#ifndef NOT_TRANSMIT
    TCPer = new TCP("119.28.6.20",8012,8011);
#endif
    QPushButton button_screenShot("snap");
    QPushButton button_quit("Quit");

    QObject::connect(&button_quit,&QPushButton::clicked,quitProg);
    QObject::connect(&button_quit,&QPushButton::clicked,&QApplication::quit);//这里对应的button属于object，而clicked是一个信号，quit是一个槽。

    QObject::connect(&button_screenShot,&QPushButton::clicked,slotGrabFullScreen);//这里对应的button属于object，而clicked是一个信号，quit是一个槽。


    button_screenShot.show();//show是一个显示该对象的类方法
    button_quit.show();

//    w.show();
    return a.exec();
}
